import dayjs from "@/utils/dayjs.ts";
import fetch from "@/api/fetch.js";
import { singleModeList } from "@/utils/game"

interface Record {
    mode: string,
    single: boolean,
    start: number,
    end: number,
    duration: number,
    total: {
        single: number,
        double: number,
        triple: number,
        tetris: number,
        tspin: number,
        tspinSingle: number,
        tspinDouble: number,
        tspinTriple: number,
        btb: number,
        btbTetris: number,
        btbTspinSingle: number,
        btbTspinDouble: number,
        btbTspinTriple: number,
        allClear: number, // 全消
        maxCombo: number,
        clearLine: number, // 累计消行
        clearRubbishLine: number, // 累计消除垃圾行数
        atkTotal: number, // 累计攻击
        getAtkTotal: number, // 累计受到攻击
        damageTotal: number, // 累计垃圾行
    },
    info: {
        apm: number,
        lpm: number,
        apl: number,
        adpm: number,
        vs: number,
    }
}

const defaultRecord = () => {
    return {
        mode: '',
        single: true,
        start: 0,
        end: 0,
        duration: 0,
        total: {
            single: 0,
            double: 0,
            triple: 0,
            tetris: 0,
            tspin: 0,
            tspinSingle: 0,
            tspinDouble: 0,
            tspinTriple: 0,
            btb: 0,
            btbTetris: 0,
            btbTspinSingle: 0,
            btbTspinDouble: 0,
            btbTspinTriple: 0,
            allClear: 0, // 全消
            maxCombo: 0,
            clearLine: 0, // 累计消行
            clearRubbishLine: 0, // 累计消除垃圾行数
            atkTotal: 0, // 累计攻击
            getAtkTotal: 0, // 累计受到攻击
            damageTotal: 0, // 累计垃圾行
        },
        info: {
            apm: 0,
            lpm: 0,
            apl: 0,
            adpm: 0,
            vs: 0,
        }
    }
}

const addRecord = (userId: string, gameResult: any) => {
    return new Promise((resolve, reject) => {
        if (!userId) reject() // addRecord: userId不存在
        let {
            gameMode,
            start,
            end,
            rafTime,
            timeFormat,
            total,
            apm,
            lpm,
            apl,
            adpm,
            vs,
        } = gameResult
        // let modeList = singleModeList()
        let single = true // 暂时默认单人模式
        let record = {
            mode: gameMode.id,
            single,
            start,
            end,
            duration: rafTime,
            timeFormat,
            total: {
                single: total.single,
                double: total.double,
                triple: total.triple,
                tetris: total.tetris,
                tspin: total.tspin,
                tspinSingle: total.tspinSingle,
                tspinDouble: total.tspinDouble,
                tspinTriple: total.tspinTriple,
                btb: total.btb,
                btbTetris: total.btbTetris,
                btbTspinSingle: total.btbTspinSingle,
                btbTspinDouble: total.btbTspinDouble,
                btbTspinTriple: total.btbTspinTriple,
                allClear: total.allClear,
                maxCombo: total.maxCombo,
                clearLine: total.clearLine,
                clearRubbishLine: total.clearRubbishLine,
                atkTotal: total.atkTotal,
                getAtkTotal: total.getAtkTotal,
                damageTotal: total.damageTotal,
            },
            info: {
                apm,
                lpm,
                apl,
                adpm,
                vs,
            }
        }
        console.log('addRecord', record)
        fetch('addRecord', {
            userId,
            record
        })
            .then((res: any) => {
                console.log('addRecord success', res);
                resolve(res) // 返回新增后的所有记录
            })
    })
}

// 导出类型 Record
export type {
    Record
}

export {
    defaultRecord,
    addRecord,
}